<template>
  <el-dialog
    title="批量添加座位"
    :visible.sync="dialogVisible"
    width="40%"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="所属区域" prop="area_id">
        <el-select
          v-model="form.area_id"
          placeholder="请选择区域"
          style="width: 100%"
          filterable
          disabled
        >
          <el-option
            v-for="item in areaOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="行数" prop="rows">
        <el-input-number v-model="form.rows" :min="1" :max="20" />
      </el-form-item>
      <el-form-item label="列数" prop="columns">
        <el-input-number v-model="form.columns" :min="1" :max="20" />
      </el-form-item>
      <el-form-item label="命名前缀" prop="prefix">
        <el-input v-model="form.prefix" placeholder="例如：A、B、座位" />
      </el-form-item>
      <el-form-item label="起始编号" prop="startNumber">
        <el-input-number v-model="form.startNumber" :min="1" :max="100" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="form.status" style="width: 100%">
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { http } from "@/api";

export default {
  name: "SeatBatchAdd",
  data() {
    return {
      dialogVisible: false,
      areaId: null,
      areaOptions: [],
      form: {
        area_id: undefined,
        rows: 5,
        columns: 5,
        prefix: "座位",
        startNumber: 1,
        status: "available"
      },
      rules: {
        area_id: [
          { required: true, message: "请选择所属区域", trigger: "change" }
        ],
        rows: [
          { required: true, message: "请输入行数", trigger: "blur" }
        ],
        columns: [
          { required: true, message: "请输入列数", trigger: "blur" }
        ],
        prefix: [
          { required: true, message: "请输入命名前缀", trigger: "blur" }
        ],
        startNumber: [
          { required: true, message: "请输入起始编号", trigger: "blur" }
        ],
        status: [
          { required: true, message: "请选择状态", trigger: "change" }
        ]
      },
      statusOptions: [
        { label: "可用", value: "available" },
        { label: "禁用", value: "disabled" }
      ]
    };
  },
  methods: {
    open(areaId, areaOptions) {
      this.dialogVisible = true;
      this.areaOptions = areaOptions;
      
      this.form = {
        area_id: areaId,
        rows: 5,
        columns: 5,
        prefix: "座位",
        startNumber: 1,
        status: "available"
      };
      
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    
    handleClose() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          try {
            await http.librarySeats.batchCreate(null, this.form);
            this.$message.success("批量添加座位成功");
            this.handleClose();
            this.$emit("get-page");
          } catch (error) {
            console.error(error);
            if (error.response && error.response.data && error.response.data.message) {
              this.$message.error(error.response.data.message);
            } else {
              this.$message.error("操作失败，请检查输入是否正确");
            }
          }
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.el-dialog {
  max-height: 90vh;
  overflow: auto;
}
.el-dialog__body {
  max-height: calc(90vh - 120px);
  overflow-y: auto;
}
</style>
